<?php
/**
 * @authors liuqinglin 
 * @email (2961181429@qq.com) 
 * @(https://my.oschina.net/shp32131)
 * @date  2016-12-03 16:35:13
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Article_model extends CI_Model{
    
    public function __construct()
    {
        parent::__construct();
        //使用默认的数据库服务器配置 连接数据库
        $this->load->database();
    }
    //插入一篇文章数据 $data 要插入的文章数据
    public function insert($data)
    {
    	if($this->db->table_exists('article'))
        {
            $mytable = $this->db->dbprefix('article');
            $insert = $this->db->insert_string($mytable,$data);
            if($this->db->query($insert))
            {
                return TRUE;
            }
        }
        return FALSE;
    }
    //获取所有的管理员 order by id desc
    public function get_data($arr,$limit = 0)
    {
        if($this->db->table_exists('article'))
        {
            $fields = implode(',', $arr);
            $mytable = $this->db->dbprefix('article');
            $select = "SELECT ".$fields.' FROM '.$mytable.' ORDER BY id DESC';
            $result = $this->db->query($select);
            if($result)
            {
                return $result->result_array();
            }
        }
        return FALSE;
    }
    //获取一条记录 $cloumn 条件字段 $value 条件字段的条件值
    public function get_one($cloumn,$value)
    {
        if($this->db->table_exists('admin'))
        {
            $mytable = $this->db->dbprefix('admin');
            $result = $this->db->query("SELECT * FROM $mytable WHERE $cloumn=$value LIMIT 1");
            return $result->row_array();
        }
        return FALSE;       
    }
    //删除一条记录 $cloumn 条件字段 $value 条件字段的值
    public function delete_one($cloumn,$value)
    {
        if($this->db->table_exists('admin'))
        {
            $mytable = $this->db->dbprefix('admin');
            if($this->db->query("DELETE FROM $mytable WHERE $cloumn=$value"))
            {
               return TRUE; 
            }
        }
        return FALSE;
    }
    //修改一条记录 $data 更新数据  $field 条件字段的 字段名 和字段值 
    public function update_one($data,$field)
    {
        if($this->db->table_exists('admin'))
        {
            $mytable = $this->db->dbprefix('admin');

            $where = $field['name']."=".$field['value'];

            $sql = $this->db->update_string($mytable,$data,$where);
            // $result = $this->db->query($sql);
            if($this->db->query($sql))
            {
                return TRUE;
            }
        }
        return FALSE;       
    }
}